home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-11-19 | 5.5 KB | 235 lines | [TEXT/MPS ] |
- /*
- File: TestArbitrator.cp
-
- Contains: xxx put contents here xxx
-
- Copyright: © 1991-1994 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
- #ifndef __TESTARBITRATOR__
- #include "TestArbitrator.h"
- #endif
- #ifndef __LIBRARYMANAGERUTILITIES__
- #include <LibraryManagerUtilities.h>
- #endif
- #ifndef __STRING__
- #include <String.h>
- #endif
-
- const unsigned long kMaxTokens = 40;
- const unsigned long kListSize = 10;
-
- unsigned long numTokens;
- Boolean useFullID;
-
-
- void _CDECL OwnerHandler(void*, EventCode, OSErrParm, void* theData)
- {
- TRequestToken* requestToken = ((TTokenNotification*) theData)->GetRequestToken();
- TToken* theToken = ((TTokenNotification*) theData)->GetToken();
-
- TTestArbitrator* theTester = (TTestArbitrator*) theToken->GetObject();
- theTester->SetOwnerHandlerCalled();
-
- requestToken->Give(theToken); // give up "our" token
- }
-
- void _CDECL RequestHandler(void*, EventCode, OSErrParm, void* theData)
- {
- TRequestToken* requestToken = ((TTokenNotification*) theData)->GetRequestToken();
- TToken* theToken = (TToken*) requestToken->GetObject();
-
- TTestArbitrator* theTester = (TTestArbitrator*) theToken->GetObject();
- theTester->SetRequestHandlerCalled();
- }
-
- /**********************************************************************
- ** PUBLIC Constructor/Destructor
- ***********************************************************************/
-
- TTestArbitrator::TTestArbitrator()
- {
- fTest = NULL;
- fCount = 0;
- }
-
- TTestArbitrator::~TTestArbitrator()
- {
- delete fTest;
- }
-
- /*******************************************************************************
- ** PUBLIC InitTest
- ********************************************************************************/
-
- void TTestArbitrator::InitTest(BooleanParm, BooleanParm, int argc, char** argv)
- {
- fTest = new TArbitrator(GetLocalPool(), (size_t) kListSize);
- numTokens = kMaxTokens;
- useFullID = false;
-
- while (argc > 0)
- {
- if ((argc >= 2) && (strcmp(*argv,"-numtokens") == 0))
- {
- argv++; argc--;
- sscanf(*argv, "%d", &numTokens);
- if (numTokens > kMaxTokens)
- numTokens = kMaxTokens;
- }
- else if (strcmp(*argv,"-fullid") == 0)
- {
- useFullID = true;
- }
- else
- {
- Printf("Usage: TestArbitrator -o -numtokens <n> -fullid\n");
- }
- argv++;
- argc--;
- }
- }
-
- void TTestArbitrator::RunTestIteration(BooleanParm verbose, BooleanParm)
- {
- char myID[128];
- TToken* myToken[kMaxTokens+1];
- int ndx;
-
- fCount++;
- if (verbose)
- Printf("Iteration = %d\n", fCount);
-
- // Register numTokens tokens
- for (ndx = 1; ndx <= numTokens; ndx++)
- {
- SLMsprintf(myID, "LPG:test$%d", ndx);
- fTest->RegisterObject(myID, NULL);
- if (verbose)
- Printf("RegisterObject called, ndx = %d, id = %s\n", ndx, myID);
- }
-
- // GetToken the tokens
- for (ndx = 1; ndx <= numTokens; ndx++)
- {
- if (useFullID)
- SLMsprintf(myID, "LPG:test$%d", ndx);
- else
- SLMsprintf(myID, "LPG:test$");
-
- TToken* theToken = fTest->GetToken(myID, kExclusiveTokenRequest);
- myToken[ndx] = theToken;
- if (theToken)
- {
- unsigned long hash = theToken->Hash();
- if (verbose)
- Printf("GetToken myToken[%d] = %x, id = %s, Hash = %d\n",
- ndx, myToken[ndx], theToken->GetID(), hash);
- }
- else
- if (verbose)
- Printf("GetToken failed, ndx = %d.\n", ndx);
- }
-
- // try to get them again - should fail
- for (ndx = 1; ndx <= numTokens; ndx++)
- {
- SLMsprintf(myID, "LPG:test$%d", ndx);
- TToken* theToken = fTest->GetToken(myID, kExclusiveTokenRequest);
- if (theToken)
- {
- if (verbose)
- Printf("GetToken exclusive request succeeded again, ndx = %d - bad news\n", ndx);
- }
- else
- if (verbose)
- Printf("GetToken exclusive request failed, ndx = %d - good news.\n", ndx);
- }
-
- // try RequestToken to get myToken[ndx] from the current "owner"
- for (ndx = 1; ndx <= numTokens; ndx++)
- {
- if (myToken[ndx])
- {
- myToken[ndx]->SetObject(this);
- fRequestHandlerCalled = false;
- fOwnerHandlerCalled = false;
-
- // Set a notifier for "LPG:test$1"
- TProcNotifier ownerNotifier(OwnerHandler);
- myToken[ndx]->SetNotifier(&ownerNotifier);
-
- // Get a notifier for the request
- TProcNotifier requestNotifier(RequestHandler);
-
- // Request token by id
- TRequestToken* myRequest = fTest->ActiveRequest(myToken[ndx]->GetID(), kExclusiveTokenRequest, &requestNotifier);
-
- if (myRequest)
- {
- TToken* theToken = NULL;
- if (fRequestHandlerCalled)
- {
- theToken = myRequest->Exchange();
- Printf("ActiveRequest succeeded, theToken = %x, token id = %s\n", theToken, theToken->GetID());
- }
- else
- {
- delete myRequest;
- Printf("ActiveRequest failed, RequestHandler not called\n");
- }
- }
- else
- {
- Printf("ActiveRequest failed, myRequest not created\n");
- }
- }
- }
-
- // release them
- for (ndx = 1; ndx <= numTokens; ndx++)
- {
- TToken* theToken = myToken[ndx];
- if (theToken)
- {
- if (verbose)
- Printf("Releasing token %x, ndx = %d\n", theToken, ndx);
- theToken->Release();
- }
- }
-
- // get them again - this time unregister and delete em
- for (ndx = 1; ndx <= numTokens; ndx++)
- {
- SLMsprintf(myID, "LPG:test$%d", ndx);
- TToken* myToken = fTest->GetToken(myID, kExclusiveTokenRequest);
- if (myToken)
- {
- unsigned long hash = myToken->Hash();
- if (verbose)
- {
- Printf("GetToken myToken = %x, Hash = %d, ndx = %d\t", myToken, hash, ndx);
- Printf("Releasing token %x,\t", myToken);
- }
- myToken->Release();
- if (verbose)
- Printf("Deleting %x\n", myToken);
- delete myToken;
- }
- else
- if (verbose)
- Printf("GetToken failed, ndx = %d.\n", ndx);
- }
- } // end RunTestIteration
-
-
- void TTestArbitrator::EndTest(BooleanParm, BooleanParm)
- {
- }
-
-